using System;
using System.IO;
using NAnt.Core;
using NAnt.Core.Attributes;

namespace LeanUi.ImageBundle
{
    [TaskName("imagebundle")]
    public class ImageBundleNantTask : Task
    {
        [TaskAttribute("basedir"), StringValidator(AllowEmpty = false)]
        public string BaseDir { get; set; }

        [TaskAttribute("outputdir"), StringValidator(AllowEmpty = true)]
        public string OutputDir { get; set; }


        [TaskAttribute("outputcss"), StringValidator(AllowEmpty = true)]
        public string OutputCssDir { get; set; }

        [TaskAttribute("outputimage"), StringValidator(AllowEmpty = true)]
        public string OutputImageDir { get; set; }

        [TaskAttribute("outputproxy"), StringValidator(AllowEmpty = true)]
        public string OutputProxyDir { get; set; }

        [TaskAttribute("outputdll"), StringValidator(AllowEmpty = true)]
        protected string OutputAssemblyDir { get; set; }

        [TaskAttribute("namespace"), StringValidator(AllowEmpty = false)]
        public string BaseNameSpace { get; set; }

        [TaskAttribute("optipng"), StringValidator(AllowEmpty = true)]
        public string OptiPng { get; set; }

        [TaskAttribute("createsinglebundle")]
        public bool CreateSingleBundle { get; set; }

        [TaskAttribute("usedisabledrule")]
        public bool UseDisabledRule { get; set; }

        [TaskAttribute("maxsize"), Int32Validator(1, Int32.MaxValue)]
        public string MaxSpriteSize { get; set; }

        [TaskAttribute("maxpixelcount"), Int32Validator(1, Int32.MaxValue)]
        protected int MaxPixelCount { get; set; }




        private DirectoryInfo ToDirInfo(string relativePath)
        {
            if (relativePath == null) return null;
            return new DirectoryInfo(Path.Combine(Properties.Project.BaseDirectory, relativePath));
        }

        private FileInfo ToFileInfo(string relativePath)
        {
            if (relativePath == null) return null;
            return new FileInfo(Path.Combine(Properties.Project.BaseDirectory, relativePath));
        }


        protected override void ExecuteTask()
        {
            try {
                Log(Level.Info, string.Format("Generating Imagebundles for {0}, saving to {1} using namespace {2}",
                    BaseDir, OutputDir, BaseNameSpace));

                //if (!Debugger.IsAttached) {
                //    Debugger.Launch();
                //}


                var gen = new ImageBundleGenerator(
                    ToDirInfo(BaseDir),
                    ToDirInfo(OutputProxyDir) ?? ToDirInfo(OutputDir),
                    ToDirInfo(OutputAssemblyDir),
                    ToDirInfo(OutputCssDir) ?? ToDirInfo(OutputDir),
                    ToDirInfo(OutputImageDir) ?? ToDirInfo(OutputDir), 
                    BaseNameSpace,
                    UseDisabledRule,
                    Log);

                gen.OptiPngPath = ToFileInfo(OptiPng);
                gen.CreateSingleBundle = CreateSingleBundle;
                int maxSpriteSize;
                gen.MaxSpriteSizeInKByte = int.TryParse(MaxSpriteSize, out maxSpriteSize) ? maxSpriteSize : (int.MaxValue / 1024)-1;

                gen.MaxPixelCount = (MaxPixelCount > 0) ? MaxPixelCount : int.MaxValue;
                gen.Generate();
            }
            catch
            {
                if (FailOnError)
                    throw;
            }
        }

    }
}